package com.sg.service.base.calc.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sg.common.util.CommonFunctionHelper;
import com.sg.dto.base.calc.req.QueryFuzzyIntegrateThemeListReq;
import com.sg.dto.base.calc.req.QueryIntegrateThemeDetailReq;
import com.sg.dto.base.calc.req.QueryIntegrateThemeListReq;
import com.sg.entity.OmsIntegrateTheme;
import com.sg.service.base.calc.MOmsIntegrateThemeService;
import com.wicket.okrcalc.common.dto.FieldInfo;
import com.wicket.okrcalc.common.exception.BizException;
import com.wicket.okrcalc.dal.mapper.mbg.OmsIntegrateThemeMapper;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author ：
 * @version 1.0
 * @since 2022/1/19 15:39
 */
@Service
public class MOmsIntegrateThemeServiceImpl
        extends ServiceImpl<OmsIntegrateThemeMapper, OmsIntegrateTheme>
        implements MOmsIntegrateThemeService {

    @Autowired
    OmsIntegrateThemeMapper omsIntegrateThemeMapper;

    /**
     * 4-2-01新增目标跨模板整合主题[10109]
     * gen by moon at 8/21/2024, 12:19:02 PM
     */
    @Trace(operationName = "4-2-01新增目标跨模板整合主题")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addIntegrateTheme(OmsIntegrateTheme omsIntegrateTheme) {
        omsIntegrateTheme.setIntegrateThemeId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
        omsIntegrateTheme.setIsValid("FALSE");
        if (super.save(omsIntegrateTheme)) {
            return omsIntegrateTheme.getIntegrateThemeId();
        } else {
            return "-1";
        }

    }

    /**
     * 4-2-01查询目标跨模板整合主题详情[10111]
     * gen by moon at 8/21/2024, 12:19:05 PM
     */
    @Trace(operationName = "4-2-01查询目标跨模板整合主题详情")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public OmsIntegrateTheme queryIntegrateThemeDetail(QueryIntegrateThemeDetailReq reqDto) {

        try {

            QueryWrapper<OmsIntegrateTheme> queryWrapper = new QueryWrapper<>();
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, new ArrayList<>());
            queryWrapper.orderByDesc("create_time");
            List<OmsIntegrateTheme> retList = super.list(queryWrapper);
            if (retList != null && retList.size() > 0) {
                OmsIntegrateTheme retData = retList.get(0);

                return retData;
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 4-2-01查询目标跨模板整合主题列表[10115]
     * gen by moon at 8/21/2024, 12:19:08 PM
     */
    @Trace(operationName = "4-2-01查询目标跨模板整合主题列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsIntegrateTheme> queryIntegrateThemeList(QueryIntegrateThemeListReq reqDto) {

        try {
            QueryWrapper<OmsIntegrateTheme> queryWrapper = new QueryWrapper<>();


            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsIntegrateTheme.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsIntegrateTheme.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsIntegrateTheme.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<OmsIntegrateTheme>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 4-2-01修改目标跨模板整合主题[10120]
     * gen by moon at 8/21/2024, 12:19:09 PM
     */
    @Trace(operationName = "4-2-01修改目标跨模板整合主题")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateIntegrateTheme(OmsIntegrateTheme omsIntegrateTheme) {
        if (omsIntegrateTheme != null && omsIntegrateTheme.getSubjectLifeCycle() != null && omsIntegrateTheme.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsIntegrateTheme.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//RELEASED_CURRENT_INDUCTIONID
        }
        if (omsIntegrateTheme != null && omsIntegrateTheme.getSubjectLifeCycle() != null && omsIntegrateTheme.getSubjectLifeCycle().equals("ARCHIVING")) {
            omsIntegrateTheme.setArchiveInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//ARCHIVE_CURRENT_INDUCTIONID
        }
        if (omsIntegrateTheme != null && omsIntegrateTheme.getSubjectLifeCycle() != null && omsIntegrateTheme.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsIntegrateTheme.setReleaseTime(CommonFunctionHelper.getCurrentTime());//RELEASE_SYSTEM_CURRENT_TIME
        }
        if (omsIntegrateTheme != null && omsIntegrateTheme.getSubjectLifeCycle() != null && omsIntegrateTheme.getSubjectLifeCycle().equals("ARCHIVING")) {
            omsIntegrateTheme.setArchiveTime(CommonFunctionHelper.getCurrentTime());//HISTORICAL_ARCHIVING
        }
        return super.updateById(omsIntegrateTheme);
    }

    /**
     * 4-2-01模糊查目标跨模板整合主题列表[10136]
     * gen by moon at 8/21/2024, 2:38:07 PM
     */
    @Trace(operationName = "4-2-01模糊查目标跨模板整合主题列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsIntegrateTheme> queryFuzzyIntegrateThemeList(QueryFuzzyIntegrateThemeListReq reqDto) {

        try {
            QueryWrapper<OmsIntegrateTheme> queryWrapper = new QueryWrapper<>();


            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsIntegrateTheme.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsIntegrateTheme.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsIntegrateTheme.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<OmsIntegrateTheme>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }
}
